package com.lyc.controller.user;

import com.lyc.DTO.PostsDTO;
import com.lyc.mapper.PostsMapper;
import com.lyc.pojo.PageBean;
import com.lyc.pojo.Post;
import com.lyc.pojo.Result;
import com.lyc.service.PostsService;
import com.lyc.utils.JwtUtil;
import com.lyc.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController// 表示该类是一个控制器
@Validated //  开启参数校验
@CrossOrigin // 添加这一行允许跨域
@RequestMapping("/post")
public class postsController {
    @Autowired
    private PostsService postsService;
    @Autowired
    private PostsMapper postsMapper;

    //添加文章
    @PostMapping("/addPosts")
    public Result addArticle(@RequestBody @Validated PostsDTO postsDTO, @RequestHeader("Authorization") String token) {
        try {
            //获取用户id
            Map<String, Object> claims = JwtUtil.parseToken(token);
            int id = (int) claims.get("id");
            // 设置作者ID
            postsDTO.setAuthorId(id);
            // 处理 metaData 字段，空字符串转换为 null 或默认 JSON
            if (postsDTO.getMetaData() != null && postsDTO.getMetaData().isEmpty()) {
                postsDTO.setMetaData(null); // 或者设置为 "{}"
            }
            //查询文章
            Post post = postsService.findByPost(postsDTO.getTitle());
            if (post != null) {
                return Result.error("文章已存在");
            }
            // 调用服务添加文章
            postsService.addPost(postsDTO);
            return Result.success("文章添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文章添加失败: " + e.getMessage());
        }
    }

    //获取文章详情
    @GetMapping("/getPostsDetail")
    public Result<Post> getPostsDetail(Long id) {
        try {
            Post post = postsService.findByPostId(id);
            if (post == null) {
                return Result.error("文章不存在");
            }
            return Result.success(post);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("文章查询失败: " + e.getMessage());
        }
    }

    //获取文章列表查询（条件分页）
    @GetMapping("/getPostsList")
    public Result <PageBean<Post>> getPostsList(Integer pageNum, Integer pageSize){
        // 参数校验
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }
        //获取当前用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        PageBean<Post> pageBean = postsService.getPostsList(pageNum, pageSize, id);

        return Result.success(pageBean);
    }

    //修改文章
    @PutMapping("/updatePosts")
    public Result updatePosts(@RequestBody @Validated PostsDTO postsDTO){
        //查询文章id
        Post post = postsService.findByPostId(postsDTO.getId());
        if (post == null) {
            return Result.error("文章不存在");
        }

        postsService.updatePosts(postsDTO);
        return Result.success("文章修改成功");
    }

    //修改文章状态
    @PatchMapping("/updateCommentStatus")
    public Result updatePostsStatus( @RequestParam String commentStatus,
                                     @RequestParam Long postId){
        postsService.updateCommentStatus(commentStatus, postId);
        return Result.success("文章状态修改成功");
    }

    //修改文章置顶
    @PatchMapping("/updateIsTop")
    public Result updateIsTop(@RequestParam Integer isTop,
                              @RequestParam Long postId){
        postsService.updateIsTop(isTop, postId);
        return Result.success("文章置顶修改成功");
    }

    //修改文章发布状态
    @PatchMapping("/updateStatus")
    public Result updateStatus(@RequestParam String status,
                               @RequestParam Long postId){
        postsService.updateStatus(status, postId);
        return Result.success("文章状态修改成功");
    }


    //删除文章
    @DeleteMapping("/deletePosts")
    public Result deletePosts(@RequestParam Long postId){
        //查询文章
        Post post = postsMapper.findByPostId(postId);
        if (post == null) {
            return Result.error("文章不存在");
        }
        postsService.deleteById(postId);
        return Result.success();
    }


}
